package com.campus.controller;

import com.campus.pojo.dto.UserPageQueryDTO;
import com.campus.pojo.dto.UserUpdatePwdDTO;
import com.campus.pojo.vo.UserVO;
import com.campus.result.PageResult;
import com.campus.result.Result;
import com.campus.pojo.dto.UserLoginDTO;
import com.campus.pojo.dto.UserRegisterDTO;
import com.campus.pojo.vo.UserLoginVO;
import com.campus.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户登录
     *
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        log.info("用户登录：{}", userLoginDTO);
        UserLoginVO userLoginVO = userService.login(userLoginDTO);
        return Result.success(userLoginVO);
    }

    /**
     * 用户注册
     *
     * @param userRegisterDTO
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody UserRegisterDTO userRegisterDTO) {
        log.info("用户注册：{}", userRegisterDTO);
        userService.register(userRegisterDTO);
        return Result.success();
    }


    /**
     * 修改密码
     *
     * @return
     */
    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody UserUpdatePwdDTO userUpdatePwdDTO) {
        log.info("修改密码：{}", userUpdatePwdDTO);
        userService.updatePassword(userUpdatePwdDTO);
        return Result.success();
    }


    /**
     * 分页查询用户
     */
    @GetMapping("/pageQuery")
    public Result<PageResult<UserVO>> pageQuery(UserPageQueryDTO userPageQueryDTO) {
        log.info("分页查询：{}", userPageQueryDTO);
        PageResult<UserVO> pageResult = userService.pageQuery(userPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 禁用/启用用户
     */
    @PutMapping("/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        log.info("修改用户账号状态 id：{} status：{}", id, status == 1 ? "启用" : "禁用");
        userService.updateStatus(id, status);
        return Result.success();
    }

    /**
     *重置密码 根据用户id
     */
    @PutMapping("/resetPassword/{id}")
    public Result resetPassword(@PathVariable Long id) {
        log.info("重置用户密码：id：{}", id);
        userService.resetPassword(id);
        return Result.success();
    }


}
